﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.RDLC

Partial Class rptTracking
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10

        LoadRptListing()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadRptListing()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadRptListing()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadRptListing()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadRptListing()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadRptListing()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadRptListing()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadRptListing()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadRptListing()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadRptListing()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadRptListing()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadRptListing()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadRptListing()
    End Sub

#End Region

    Private Function LoadListing() As Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        Dim strWhere As String
        Dim strSelectDate As String
        Dim strUNIONALL As String
        Dim strWho As String
        strUNIONALL = ""
        strWho = ""

        Dim dtFromDate As DateTime
        Dim dtToDate As DateTime
        'to add in filtering for report - fromDate, toDate, Type(Created;Updated)
        If txtFromDate.Text <> "" And txtToDate.Text <> "" Then
            dtFromDate = Date.ParseExact(txtFromDate.Text, "dd-MMM-yyyy", Nothing)
            dtToDate = Date.ParseExact(txtToDate.Text, "dd-MMM-yyyy", Nothing)
            If dtFromDate > dtToDate Then
                lblMsgError.Text = "Date To cannot be earlier than Date From!"
                divError.Visible = True
                Return Nothing
                Exit Function
            Else
                strSelectDate = "CASE ISNULL(p.UpdateDate,'') WHEN '' THEN p.CreateDate ELSE p.UpdateDate END as 'Date', " & _
                                "CASE ISNULL(p.UpdateDate,'') WHEN '' THEN 'Created' ELSE 'Updated' END as 'Type', "    'Edit by Tan Chin Teong 20120429
                dtFromDate = Date.ParseExact(txtFromDate.Text, "dd-MMM-yyyy", Nothing)
                dtToDate = Date.ParseExact(txtToDate.Text, "dd-MMM-yyyy", Nothing)

                Dim dtStartTxt As String
                Dim dtEndTxt As String
                dtStartTxt = dtFromDate.Year.ToString + IIf(dtFromDate.Month > 9, dtFromDate.Month.ToString, "0" + dtFromDate.Month.ToString) + IIf(dtFromDate.Day > 9, dtFromDate.Day.ToString, "0" + dtFromDate.Day.ToString)
                dtEndTxt = dtToDate.Year.ToString + IIf(dtToDate.Month > 9, dtToDate.Month.ToString, "0" + dtToDate.Month.ToString) + IIf(dtToDate.Day > 9, dtToDate.Day.ToString, "0" + dtToDate.Day.ToString)

                If ddlStaffName.SelectedValue = "" Then '--All
                    strWhere = ""
                Else
                    strWhere = " and (secondtable.updatestaff = @StaffId or " & _
                               "firstable.insertstaff = @StaffId) "
                    objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(ddlStaffName.SelectedValue))
                End If

                objDB.OpenConnection()

                objDB.dbVariable.SQLString = "select * from ( select newentry2.insertcount,newentry2.onlydate as insertdate, newentry2.staff insertstaff, " & _
                    "stafftable.username as insertusername from ( select count(propertyid) as insertcount, staff,onlydate from " & _
                    "( (select ([" & AppSettings("database").ToString & "].dbo.AW_Property.PropertyId) as propertyid, [" & AppSettings("database").ToString & "].dbo.AW_Property.SListerId as staff,CONVERT(VARCHAR(10),[" & AppSettings("database").ToString & "].dbo.AW_Property.SCreateDate,112) as onlydate " & _
                    "from [" & AppSettings("database").ToString & "].dbo.AW_Property " & _
                    "where [" & AppSettings("database").ToString & "].dbo.AW_Property.SListerId is not null and CONVERT(VARCHAR(10),[" & AppSettings("database").ToString & "].dbo.AW_Property.SCreateDate,112) is not null) " & _
                    "UNION ALL(select [" & AppSettings("database").ToString & "].dbo.AW_Property.PropertyId as propertyid, " & _
                    "[" & AppSettings("database").ToString & "].dbo.AW_Property.RListerId as staff, CONVERT(VARCHAR(10),[" & AppSettings("database").ToString & "].dbo.AW_Property.RCreateDate,112) as onlydate " & _
                    "from [" & AppSettings("database").ToString & "].dbo.AW_Property " & _
                    "where [" & AppSettings("database").ToString & "].dbo.AW_Property.RListerId is not null and CONVERT(VARCHAR(10)," & _
                    "[" & AppSettings("database").ToString & "].dbo.AW_Property.RCreateDate,112) is not null) )" & _
                    "newentry group by staff, onlydate) newentry2 " & _
                    "left join " & AppSettings("database").ToString & ".dbo.AW_Staff stafftable on stafftable.StaffId = newentry2.staff)firstable " & _
                    "full outer join " & _
                    "(select updateentry.updatecount,updateentry.staff as updatestaff,updateentry.onlydate as updatedate,stafftable.UserName as updateusername " & _
                    "from (select count(1) as updatecount, A.staff, A.onlydate " & _
                    "from (select [" & AppSettings("logdatasource").ToString & "].dbo.AW_Property.propertyid as property, " & _
                    "[" & AppSettings("logdatasource").ToString & "].dbo.AW_Property.UpdateWho as staff,CONVERT(VARCHAR(10)," & _
                    "[" & AppSettings("logdatasource").ToString & "].dbo.AW_Property.CreateDate,112) as onlydate from [" & AppSettings("logdatasource").ToString & "].dbo.AW_Property) as A " & _
                    "group by A.staff,A.onlydate) updateentry " & _
                    "left join " & AppSettings("database").ToString & ".dbo.AW_Staff stafftable on stafftable.StaffId = updateentry.staff) secondtable " & _
                    "on firstable.insertdate = secondtable.updatedate  and firstable.insertstaff = secondtable.updatestaff " & _
                    "where (firstable.insertdate>= " & dtStartTxt & " AND firstable.insertdate <= " & dtEndTxt & " or " & _
                    "secondtable.updatedate>= " & dtStartTxt & " AND secondtable.updatedate <= " & dtEndTxt & ") " & _
                    strWhere & _
                    lblHidSortType.Text


                objDB.FillData(TempDT, "AW_Property", Database.SQLCommandType.NormalString)
                objDB.CloseConnection()
                'Creating a Paged Datasouce
                Return TempDT
            End If
        Else
            lblMsgError.Text = "Please select a date range!"
            divError.Visible = True
            Return Nothing
            Exit Function
        End If
    End Function

    Private Sub LoadRptListing()

        Dim TempDT As New Data.DataTable

        TempDT = LoadListing()

        If TempDT IsNot Nothing Then
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repListing.DataSource = objPaged
            repListing.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadRptListing()
    End Sub

    Protected Sub btnResetSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnResetSearch.Click
        ClearAllFields()
    End Sub

    Private Sub ClearAllFields()
        txtFromDate.Text = String.Empty
        txtToDate.Text = String.Empty
        'ddlStatus.SelectedIndex = -1
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub imgGenerate_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgGenerate.Click
        Dim TempDT As New Data.DataTable

        TempDT = LoadListing()

        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptListing.rdlc", "ListingReport")
            TempDT.TableName = "dsListing_AW_Property"
            objRDLC.AddDatabase(TempDT)
            'If ddlStatus.SelectedValue = 0 Then
            '    objRDLC.AddParameter("ReportType", "Sale")
            'ElseIf ddlStatus.SelectedValue = 1 Then
            '    objRDLC.AddParameter("ReportType", "Rent")
            'ElseIf ddlStatus.SelectedValue = 2 Then
            '    objRDLC.AddParameter("ReportType", "Sale & Rent")
            'End If
            objRDLC.ExportReport(RDLC.ExportTypeFormat.PDF, HttpContext.Current)
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then
            GetddlListBy()
        End If

    End Sub

    'Edit by Tan Chin Teong 20120712
    Private Sub GetddlListBy()
        Dim TempDT As New Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        objDB.OpenConnection()

        objDB.dbVariable.SQLString = "SELECT StaffId,Name FROM AW_Staff WHERE StatusDelete = 0 ORDER BY Name"
        objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)

        objDB.CloseConnection()

        With ddlStaffName
            .DataSource = TempDT
            .DataTextField = "Name"
            .DataValueField = "StaffId"
            .DataBind()
            .Items.Insert(0, "ANY")
            .Items(0).Value = ""
        End With
    End Sub

    Protected Sub lnkinsertusername_Click(sender As Object, e As EventArgs) Handles lnkinsertusername.Click
        If lnkinsertusername.Text = "Lister Name▼" Then
            lnkinsertusername.Text = "Lister Name▲"
        Else
            lnkinsertusername.Text = "Lister Name▼"
        End If

        lblHidSortType.Text = " ORDER BY insertusername " & SortType(lnkinsertusername.Text) & ", insertdate " & SortType(lnkinsertdate.Text)
        LoadRptListing()
    End Sub

    Protected Sub lnkinsertdate_Click(sender As Object, e As EventArgs) Handles lnkinsertdate.Click
        If lnkinsertdate.Text = "Listing Date▼" Then
            lnkinsertdate.Text = "Listing Date▲"
        Else
            lnkinsertdate.Text = "Listing Date▼"
        End If

        lblHidSortType.Text = " ORDER BY insertdate " & SortType(lnkinsertdate.Text) & ", insertusername " & SortType(lnkinsertusername.Text)
        LoadRptListing()
    End Sub

    Protected Sub lnkinsertcount_Click(sender As Object, e As EventArgs) Handles lnkinsertcount.Click

    End Sub

    Protected Sub lnkupdateusername_Click(sender As Object, e As EventArgs) Handles lnkupdateusername.Click
        If lnkupdateusername.Text = "Updater Name▼" Then
            lnkupdateusername.Text = "Updater Name▲"
        Else
            lnkupdateusername.Text = "Updater Name▼"
        End If

        lblHidSortType.Text = " ORDER BY updateusername " & SortType(lnkupdateusername.Text) & ", updatedate " & SortType(lnkupdatedate.Text)
        LoadRptListing()
    End Sub

    Protected Sub lnkupdatedate_Click(sender As Object, e As EventArgs) Handles lnkupdatedate.Click
        If lnkupdatedate.Text = "Listing Date▼" Then
            lnkupdatedate.Text = "Listing Date▲"
        Else
            lnkupdatedate.Text = "Listing Date▼"
        End If

        lblHidSortType.Text = " ORDER BY updatedate " & SortType(lnkupdatedate.Text) & ", updateusername " & SortType(lnkupdateusername.Text)
        LoadRptListing()
    End Sub

    Protected Sub lnkupdatecount_Click(sender As Object, e As EventArgs) Handles lnkupdatecount.Click

    End Sub
End Class


